{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Getting Financial Data - Google Finance"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Introduction:\n",
    "\n",
    "This time you will get data from a website.\n",
    "\n",
    "\n",
    "### Step 1. Import the necessary libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "# package to extract data from various Internet sources into a DataFrame\n",
    "# make sure you have it installed\n",
    "from pandas_datareader import data, wb\n",
    "\n",
    "# package for dates\n",
    "import datetime as dt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 2. Create your time range (start and end variables). The start date should be 01/01/2015 and the end should today (whatever your today is)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.datetime(2015, 1, 1, 0, 0)"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "start = dt.datetime(2015, 1, 1)\n",
    "\n",
    "end = dt.datetime.today()\n",
    "\n",
    "start"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 3. Select the Apple, Tesla, Twitter, IBM, LinkedIn stocks symbols and assign them to a variable called stocks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "stocks = ['AAPL', 'TSLA', 'IBM', 'LNKD']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 4. Read the data from google, assign to df and print it"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<class 'pandas.core.panel.Panel'>\n",
       "Dimensions: 5 (items) x 399 (major_axis) x 4 (minor_axis)\n",
       "Items axis: Open to Volume\n",
       "Major_axis axis: 2015-01-02 00:00:00 to 2016-08-02 00:00:00\n",
       "Minor_axis axis: AAPL to TSLA"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = web.DataReader(stocks, 'google', start, end)\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 5.  What is the type of structure of df ?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 'pandas.core.panel.Panel'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 6. Print all the Items axis values\n",
    "#### To learn more about the Panel structure go to [documentation](http://pandas.pydata.org/pandas-docs/stable/dsintro.html#panel) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index([u'Open', u'High', u'Low', u'Close', u'Volume'], dtype='object')"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.items"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 7. Good, now we know  the data avaiable. Create a dataFrame called vol, with the Volume values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AAPL</th>\n",
       "      <th>IBM</th>\n",
       "      <th>LNKD</th>\n",
       "      <th>TSLA</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015-01-02</th>\n",
       "      <td>53204626.0</td>\n",
       "      <td>5525466.0</td>\n",
       "      <td>1203743.0</td>\n",
       "      <td>4764443.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-05</th>\n",
       "      <td>64285491.0</td>\n",
       "      <td>4880389.0</td>\n",
       "      <td>1400562.0</td>\n",
       "      <td>5368477.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-06</th>\n",
       "      <td>65797116.0</td>\n",
       "      <td>6146712.0</td>\n",
       "      <td>2006546.0</td>\n",
       "      <td>6261936.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-07</th>\n",
       "      <td>40105934.0</td>\n",
       "      <td>4701839.0</td>\n",
       "      <td>985016.0</td>\n",
       "      <td>2968390.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-01-08</th>\n",
       "      <td>59364547.0</td>\n",
       "      <td>4241113.0</td>\n",
       "      <td>1293955.0</td>\n",
       "      <td>3442509.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  AAPL        IBM       LNKD       TSLA\n",
       "Date                                                   \n",
       "2015-01-02  53204626.0  5525466.0  1203743.0  4764443.0\n",
       "2015-01-05  64285491.0  4880389.0  1400562.0  5368477.0\n",
       "2015-01-06  65797116.0  6146712.0  2006546.0  6261936.0\n",
       "2015-01-07  40105934.0  4701839.0   985016.0  2968390.0\n",
       "2015-01-08  59364547.0  4241113.0  1293955.0  3442509.0"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vol = df['Volume']\n",
    "vol.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 8. Aggregate the data of Volume to weekly\n",
    "#### Hint: Be careful to not sum data from the same week of 2015 and other years."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>AAPL</th>\n",
       "      <th>IBM</th>\n",
       "      <th>LNKD</th>\n",
       "      <th>TSLA</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>week</th>\n",
       "      <th>year</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">1</th>\n",
       "      <th>2015</th>\n",
       "      <td>53204626.0</td>\n",
       "      <td>5525466.0</td>\n",
       "      <td>1203743.0</td>\n",
       "      <td>4764443.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016</th>\n",
       "      <td>343422014.0</td>\n",
       "      <td>25233098.0</td>\n",
       "      <td>6630485.0</td>\n",
       "      <td>20967926.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">2</th>\n",
       "      <th>2015</th>\n",
       "      <td>283252615.0</td>\n",
       "      <td>24458400.0</td>\n",
       "      <td>7203125.0</td>\n",
       "      <td>22709607.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016</th>\n",
       "      <td>302072797.0</td>\n",
       "      <td>29379214.0</td>\n",
       "      <td>9160521.0</td>\n",
       "      <td>22997290.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <th>2015</th>\n",
       "      <td>304226647.0</td>\n",
       "      <td>23263206.0</td>\n",
       "      <td>7084168.0</td>\n",
       "      <td>30799137.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  AAPL         IBM       LNKD        TSLA\n",
       "week year                                                \n",
       "1    2015   53204626.0   5525466.0  1203743.0   4764443.0\n",
       "     2016  343422014.0  25233098.0  6630485.0  20967926.0\n",
       "2    2015  283252615.0  24458400.0  7203125.0  22709607.0\n",
       "     2016  302072797.0  29379214.0  9160521.0  22997290.0\n",
       "3    2015  304226647.0  23263206.0  7084168.0  30799137.0"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vol['week'] = vol.index.week\n",
    "vol['year'] = vol.index.year\n",
    "\n",
    "week = vol.groupby(['week','year']).sum()\n",
    "week.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 9. Find all the volume traded in the year of 2015"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AAPL</th>\n",
       "      <th>IBM</th>\n",
       "      <th>LNKD</th>\n",
       "      <th>TSLA</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>year</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2015</th>\n",
       "      <td>1.301994e+10</td>\n",
       "      <td>1.100959e+09</td>\n",
       "      <td>440376163.0</td>\n",
       "      <td>1.085839e+09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016</th>\n",
       "      <td>6.081474e+09</td>\n",
       "      <td>6.585250e+08</td>\n",
       "      <td>453233878.0</td>\n",
       "      <td>7.540962e+08</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              AAPL           IBM         LNKD          TSLA\n",
       "year                                                       \n",
       "2015  1.301994e+10  1.100959e+09  440376163.0  1.085839e+09\n",
       "2016  6.081474e+09  6.585250e+08  453233878.0  7.540962e+08"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "del vol['week']\n",
    "vol['year'] = vol.index.year\n",
    "\n",
    "year = vol.groupby(['year']).sum()\n",
    "year"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### BONUS: Create your own question and answer it."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
